<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>品牌管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
	<script src="../js/pages/public.js"></script>
    
</head>
<body class="hold-transition skin-red sidebar-mini">
  <!-- .box-body -->
                    <div class="box-header with-border">
                        <h3 class="box-title">品牌管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
										<button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#addModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="del()"><i class="fa fa-trash-o"></i> 删除</button>
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
									<div>
										<form id="selectForm1">
											<input name="name" class="form-control" placeholder="品牌名称">
											<input name="firstChar" class="form-control" placeholder="品牌首字母">
											<input type="button" value="搜索" onclick="load()">
										</form>
									</div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                                         
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">品牌ID</th>
									      <th class="sorting">品牌名称</th>									      
									      <th class="sorting">品牌首字母</th>									     				
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="brand">

			                      </tbody>
			                  </table>
							<a href="javascript:getpage(1)">首页</a>
							<a href="javascript:getpage(2)">上一页</a>
							<a>当前第<input name="pageNum" size="1" id="pageNum" value="1">页
								<a>每页显示<input id="pageSize" size="1">条记录共<input size="1" id="allCount">条记录</a>
								<a href="javascript:getpage(3)">下一页</a>
								<a href="javascript:getpage(4)">末页</a>
							</a>
			                  <!--数据列表/-->                        
							  
							 
                        </div>
						<!-- 数据表格 /-->
						<div id="pageInfo" style="background:#FFF;padding-left:15px;">
						</div>
                        
                     </div>
                    <!-- /.box-body -->
         
<!-- 增加窗口 -->
  <!-- 编辑窗口 -->
  <form id="addfrom">
	  <div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		  <div class="modal-dialog" >
			  <div class="modal-content">
				  <div class="modal-header">
					  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
					  <h3 id="myModalLabel">品牌编辑</h3>
				  </div>
				  <div class="modal-body">
					  <table class="table table-bordered table-striped"  width="800px">
						  <tr>
							  <td>品牌名称</td>
							  <td><input  class="form-control" placeholder="品牌名称" name="name" id="name">  </td>
						  </tr>
						  <tr>
							  <td>首字母</td>
							  <td><input  class="form-control" placeholder="首字母" name="firstChar" id="firstChar">  </td>
						  </tr>
					  </table>
				  </div>
				  <div class="modal-footer">
					  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="add()">保存</button>
					  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
				  </div>
			  </div>
		  </div>
	  </div>
  </form>

  <!-- 修改窗口 -->
  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel1">品牌编辑</h3>
			  </div>
			  <from id="updatefrom">
				  <div class="modal-body">
					  <table class="table table-bordered table-striped" width="800px" id="brandvalue">

					  </table>
				  </div>
			  </from>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updatebrand()">修改</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>

	<script>
		var token = window.localStorage.getItem("token");
		$(function() {
			select();
			getpage(1);
		})
		function load() {
			select();
		}

		function getpage(id) {
			var dang = parseInt($("#pageNum").val());
			var gong = parseInt($("#allCount").val());
			var zong = parseInt($("#pageSize").val());
			var o =gong-(gong%zong==0? gong/zong : gong%zong);
			var mo = (o/zong)+1;
			if (id==1){
				$("#pageNum").val(1);
			}else if (id==2){
				$("#pageNum").val(dang-1);
			}else if (id==3){
				$("#pageNum").val(dang+1);
			}else if (id==4){
				$("#pageNum").val(mo);
			}
			select()
		}
		function select() {
			//获取条件查询的值
			var parms = $("#selectForm1").serializeObject();
			var page = {"pageNum":$("#pageNum").val(),"pageSize":5,"parms":parms};
			$.ajax({
				headers: {"AutharizeToken":token},
				url: domain+"/brand/findBrandPage",
				contentType: 'application/json;charset=utf-8',
				type:"post",
				dataType:"json",
				//将传到后台的数据转为json格式
				data: JSON.stringify(page),
				//异步请求
				async:false,
				success:function (result) {
					console.log(result)
					if (result.code==10000){
						var list = result.data.rows;
						//拼接路径
						var  html = '';
						$(list).each(function(i,e) {
							html += '	<tr>'
							html += '	<td><input name="checkbox" value="'+e.id+'" type="checkbox" ></td>'
							html += '	<td>'	+ e.id+     '</td>'
							html += '	<td>'	+ e.name+   '</td>'
							html += '	<td>'	+ e.firstChar+'</td>'
							html += '	<td className="text-center" align="center">'
							html += '	<button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#updateModal" onclick="toupdatebrand('+e.id+')" >修改</button> '
							html += '	</td>'
							html += '	</tr>'
						})
						$("#brand").html(html)
						$("#pageSize").val(result.data.pageSize);
						$("#pageNum").val(result.data.pageNum);
						$("#allCount").val(result.data.total);
					}else if (result.code === 10003) {
						alert(result.message)
						location.href=login.html
					}
				},error:function () {
					alert("系统异常")
				}
			})
		}

		//去修改
		function toupdatebrand(id) {
			$.ajax({
				headers : {"AutharizeToken":token},
				url:domain+"/brand/getById",
				type : "POST",
				data:{id:id},
				dataType : "json",
				/*contentType : "application/json;charset=utf-8",*/
				async : false,
				success : function(result) {
				    var br = "<tr><input name='id' type='hidden' id='id' value='"+result.data.id+"'>" +
							"<td>品牌名称</td><td>" +
							"<input class='form-control' name='name' id='cname' placeholder='品牌名称' value='" + result.data.name + "'>" +
							"</td>" +
							"</tr>" +
							"<tr>" +
							"<td>首字母</td><td>" +
							"<input class='form-control' name='firstChar' id='cfirstChar' placeholder='首字母' value='" + result.data.firstChar + "'>" +
							"</td>" +
							"</tr>"
					$("#brandvalue").html(br);
				},error : function() {
				    alert("系统错误")
				}
			})
		}

		function updatebrand() {
			var parms = {
				"id": $("#id").val(),
				"name":$("#cname").val(),
				"firstChar": $("#cfirstChar").val()
			}
			$.ajax({
				url: domain + "/brand/updateBranVO",
				headers : {"AutharizeToken":token},
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				type: "post",
				async: false,
				data: JSON.stringify(parms),
				success: function (result) {
					if (result.code == 10000) {
						alert("修改成功")
						location.reload();
					}
				}, error: function () {
					alert("系统错误")
				}
			})
		}
		
		//增加
		function add() {
			var parms = $("#addfrom").serializeObject();
			$.ajax({
				url: domain+"/brand/addBrandVO",		  //请求的路径，获取数据源
				headers : {"AutharizeToken":token},
				contentType: 'application/json;charset=utf-8',
				async:false,
				type: "post",			  //请求的方式
				dataType: "json",		  //请求返回的数据类型
				data:JSON.stringify(parms),
				success: function (result) {
					if (result.code==10000){
						alert("增加成功")
						window.location.reload();
					}
				},
				error : function(result) {
					alert("增加失败!")
				}
			})
		}


		function del() {
			var ids = [];
			var checkbox=$("input[name=checkbox]");
			$(checkbox).each(function (i,v) {
				if ($(v).prop("checked")){
					ids.push($(v).val())
				}
			})
			if (ids.length <= 0) {
				alert("请选择要删除的id");
				return
			}
			if (confirm("是否确定要删除")) {
				$.ajax({
					headers : {"AutharizeToken":token},
					url: domain + "/brand/delete?ids=" + ids,
					contentType: "application/json; charset=utf-8",
					dataType: "json",
					type: "delete",
					async: false,
					success: function (result) {
						location.reload();
					}, error: function () {
						alert("系统错误")
					}
				})
			}
		}
	</script>
</body>
</html>